package think.in.java.concurrency.PractiseOneMoreTime;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.*;

/**
 * Create a task that sleeps for a random amount of time between 1 and 10 seconds,
 * then displays its sleep time and exits.
 * Create and run a quantity (given on the command line) of these tasks.
 */
public class Exec6RandomSleepTime implements Callable<Integer> {
    private int randomSleepTime =0;
    public Exec6RandomSleepTime(Integer sleepTime){
        randomSleepTime = sleepTime;

    }
    @Override
    public Integer call() throws Exception {
        TimeUnit.SECONDS.sleep(randomSleepTime);
        return randomSleepTime;
    }

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        ExecutorService executorService = Executors.newFixedThreadPool(3);
        List<Future<Integer>> futureList = new ArrayList<Future<Integer>>();
        Random random = new Random(100);
        for(int i =0; i < 3; i++)
            futureList.add(executorService.submit(new Exec6RandomSleepTime((random.nextInt())%10)));
        int threadNumber = 0;
        for(Future<Integer> future : futureList){
            threadNumber++;
            System.out.println("Thread :" + threadNumber + " sleep " + future.get() + " second.");
        }
        executorService.shutdown();
    }
}
